// pages/visitor/history/history.js
var api = require('../../../utils/httpUtil.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabCur: 0,
    scrollLeft: 0,
    tabList: ['已通过', '已拒绝'],
    openId: app.globalData.openId,
    pIndex: 0,
    status: ['通过', '拒绝'],
    visitorId: null,
    message: '',
    visitorList: [],
    queryData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadData(1);
    this.loadData(2 );
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  loadData(appStatus) {
    api.httpRequest(api.visitorListAPI, {
        staffOpenId: this.data.openId,
        status: appStatus
      })
      .then(res => {
        if (res.code == 200) {
          if (appStatus === 1) {
            this.setData({
              visitorList: res.data
            })
          } else {
            this.setData({
              visitorList1: res.data
            })
          }

        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }

      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.id,
    })
  },

  showModal(e) {
    this.setData({
      showCancel: true,
      visitorId: e.currentTarget.dataset.id
    })
  },
  hideModal(e) {
    this.setData({
      showCancel: false
    })
  },

  statusChange(e) {
    this.setData({
      pIndex: e.detail.value
    })
  },

  textareaInput(e) {
    this.setData({
      message: e.detail.value
    })
  },

  confirmClick(e) {
    api.httpRequest(api.visitorApproveAPI, {
        visitorId: this.data.visitorId,
        status: this.data.pIndex + 1,
        message: this.data.message,
        hyperlink: '/pages/visitor/center/center'
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          duration: 2000,
          mask: true,
          icon: 'none'
        })
        if (res.code == 200) {
          this.setData({
            showCancel: false
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  }
})